export default {
	data() {
		return {
			//数据
			uploadNum: 0,
			FileUpload: [],
			validate: {}, //验证对象
      briefing: {} ,//简报信息
      tempName: '',
			hasUuid:"",//有值表示修改，无值表示新增
			fatherIndex: 0, //一级菜单index
			errorEnclosure:false//是否上传附件，提示语显示
		}
	},
	created() { //调用方法
	},
	updated:function(){
		if(this.getParamValue("fatherIndex")){
			this.fatherIndex=this.getParamValue("fatherIndex");
		}
		if(this.getParamValue("childIndex")){
			this.childIndex=this.getParamValue("childIndex");
		}
	},
	mounted() {
		let that = this;

    let uuid = that.get("briefingid", uuid);
    that.hasUuid=uuid;
    if(uuid != null && uuid != "") {
      that.$post(
        that.urls.getBriefing, {
          "uuid":uuid
        }
      )
        .then(function(res) {
          if(res.success) {
            that.briefing = res.data;
            if (res.data != null) {
              that.tempName = res.data.name;
            }
            $("#enclosure").html(that.getFileName(that.briefing.enclosureUrl));
          }
        })
    }

		//校验名称重复
		$.validator.addMethod("checkNameRepeat", function(value, element, params) {
			let flg = false;
			if(that.briefing.id != null && (value == that.tempName)) {
				flg = true;
			} else {
				$.ajax({
					type: "POST",
					async: false,
					url: that.urls.checkBriefingName,
					data: JSON.stringify({
						"name": value
					}),
					dataType: "json",
					contentType: "application/json",
					success: function(data) {
						if(data.success) {
							flg = !data.data;
						}
					}
				});
			}
			return this.optional(element) || flg;
		}, "该标题已存在！");

		//加载表单验证
		that.validate = $("#briefingForm").validate({
			ignore: [],
			rules: {
				name: { // 简报名称
					required: true,
					maxlength: 50,
					checkNameRepeat: true
				},
        /*enclosureUrl: { // 简报附件
					required: true,
					maxlength: 500
				}*/
			},
			messages: {
				name: { // 简报名称
					required: "请输入标题",
					maxlength: "标题最多只能输入50个字"
				},
       /* enclosureUrl: { // 简报附件
					required: "请选择文件",
					maxlength: "文件长度最多只能输入500个字"
				}*/
			},
//			errorElement: "em",
//			errorPlacement: function(error, element) {
//				error.appendTo(element.parents(".error-box").children(".error-msg"));
//			}
		});
	},
	methods: { //写函数方法
		touchOffIdInput(n) {
			this.uploadNum = n;
			if(n == 1) {
				$("#handIdFront").click();
			}
		},
    //删除附件
    deleteEnclosure: function() {
      $("#enclosure").html("");
      $("input[name='enclosureUrl']").val("");
    },
		/**文件上传js**/
		idFrontImg: function(event) {
			var that = this;
			var files = event.currentTarget.files[0];
      if (!that.fileGlobalFilter(files)) {
        return;
      }
			let formData = new FormData();
      formData.append('fileNames', files);
			let config = {
				headers: {
					'Content-Type': 'multipart/form-data;boundary = ' + new Date().getTime()
				}
			};
			that.$post(
					that.urls.upload,
					formData,
					config
				)
				.then(function(res) {
					if(res.success) {
						let fileInfo = res.data[0];
						$("input[name='enclosureUrl']").val(fileInfo.url);
						$("#enclosure").html(that.getFileName(fileInfo.url));
						that.errorEnclosure=false;
					}
				})
		},
		//保存并发布
		saveDownload: function() {
			let that = this;
			let flg = that.validate.form();
			let enclosure=$("input[name='enclosureUrl']").val();
			if(!enclosure){
				this.errorEnclosure=true;
				return;
			}
			this.errorEnclosure=false;
			if(flg) {
				let dataObj = that.combinationFormObject(that.validate.currentForm);
				that.$post(
						that.urls.addBriefing,
						dataObj
					)
					.then(function(res) {
						let msg = "";
						if(res.success) {
							msg = "发布成功！";
							that.briefing = res.data;
              that.backBriefing();
						} else {
							msg = "发布失败！";
						}
						layer.msg(msg, {
							time: 3000, //3s后自动关闭
						});
					})
			}
		},
		//更新
		updateDownload: function() {
			let that = this;
			let flg = that.validate.form();
			let enclosure=$("input[name='enclosureUrl']").val();
			if(!enclosure){
				this.errorEnclosure=true;
				return;
			}
			this.errorEnclosure=false;
			if(flg) {
				let dataObj = that.combinationFormObject(that.validate.currentForm);
				that.$post(
						that.urls.updateBriefing,
						dataObj
					)
					.then(function(res) {
						let msg = "";
						if(res.success) {
							msg = "保存成功！";
              that.backBriefing();
						} else {
							msg = "保存失败！";
						}
						layer.msg(msg, {
							time: 3000, //3s后自动关闭
						});
					})
			}
		},
    /*跳转到简报列表*/
    backBriefing: function() {
      var that = this;
      that.$router.push({
        path: "briefReport?fatherIndex="+this.fatherIndex,
      })
    },
	}
}
